package com.aspose.cells;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

class zarw {
   private DigitalSignatureCollection a;
   private zbbm b;
   private zarq c;
   private DigitalSignature d;
   private com.aspose.cells.b.a.g.a.ze e = new com.aspose.cells.b.a.g.a.ze("sig(\\d+)(\\x2Exml)$");
   private static final byte[] f = new byte[4194304];
   private static String[] g = new String[]{"_rels/", "_xmlsignatures", "[Content_Types].xml"};

   public zarw(DigitalSignatureCollection var1) throws Exception {
      if (var1 == null) {
         throw new Exception("Signatures can not be null");
      } else {
         this.a = var1;
         this.c = new zarq("DigestMethod");
         com.aspose.cells.b.a.a.ze.a(this.c.a, (Object)(new zakr("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1")));
      }
   }

   public void a(com.aspose.cells.b.a.d.zm var1) throws Exception {
      this.b(var1);
   }

   private byte[] a(byte[] var1) {
      int var3 = var1.length;
      byte[] var2;
      if (var1[0] == 0) {
         var2 = new byte[var1.length - 1];
         System.arraycopy(var1, 1, var2, 0, var3 - 1);
      } else {
         var2 = var1;
      }

      return var2;
   }

   private String b(byte[] var1) {
      try {
         StringBuffer var2 = new StringBuffer(1024);
         PKCS8EncodedKeySpec var3 = new PKCS8EncodedKeySpec(var1);
         KeyFactory var4 = KeyFactory.getInstance("RSA");
         RSAPrivateCrtKey var5 = (RSAPrivateCrtKey)var4.generatePrivate(var3);
         var2.append("<RSAKeyValue>");
         var2.append("<Modulus>");
         var2.append(com.aspose.cells.b.a.ze.a(this.a(var5.getModulus().toByteArray())));
         var2.append("</Modulus>");
         var2.append("<Exponent>");
         var2.append(com.aspose.cells.b.a.ze.a(this.a(var5.getPublicExponent().toByteArray())));
         var2.append("</Exponent>");
         var2.append("</RSAKeyValue>");
         return var2.toString().replaceAll("[ \t\n\r]", "");
      } catch (Exception var6) {
         System.err.println(var6);
         return null;
      }
   }

   private zars a(DigitalSignature var1) throws Exception {
      zars var2 = new zars("KeyInfo", "");
      String var3 = this.b(var1.b().getEncoded());
      zars var4 = new zars("KeyValue", var3);
      var2.a((zart)var4);
      String var5 = com.aspose.cells.b.a.ze.a(var1.a());
      zars var6 = new zars("X509Data", "<X509Certificate>" + var5 + "</X509Certificate>");
      var2.a((zart)var6);
      return var2;
   }

   private zars a(com.aspose.cells.a.f.zi var1, String var2, String var3) throws Exception {
      if (var2 != null && var2.length() > 0) {
         com.aspose.cells.a.f.zh var4 = var1.a(var2);
         if (var4.a() <= 0L) {
            return null;
         } else {
            com.aspose.cells.b.a.d.zm var5 = var1.a(var4);
            zbbm var10000 = this.b;
            byte[] var6 = zbbm.a(var5);
            zars var7 = new zars("Reference", "");
            com.aspose.cells.b.a.a.ze.a(var7.b, (Object)(new zakr("URI", var3)));
            var7.a((zart)this.c);
            zars var8 = new zars("DigestValue", com.aspose.cells.b.a.ze.a(var6));
            var7.a((zart)var8);
            return var7;
         }
      } else {
         return null;
      }
   }

   private zars a() {
      DateTime var1 = this.d.getSignTime();
      String var2 = var1.b("yyyy-MM-dd") + "T" + var1.b("HH:mm:ss") + "Z";
      String var3 = "<SignatureProperty Id=\"idSignatureTime\" Target=\"#idPackageSignature\"><mdssi:SignatureTime><mdssi:Format>YYYY-MM-DDThh:mm:ssTZD</mdssi:Format><mdssi:Value>" + var2 + "</mdssi:Value>" + "</mdssi:SignatureTime></SignatureProperty>";
      zars var4 = new zars("SignatureProperties", var3);
      return var4;
   }

   private zars a(com.aspose.cells.a.f.zi var1, com.aspose.cells.a.f.zh var2) throws Exception {
      zcfk var3 = new zcfk();
      var3.a(var1.a(var2));
      zars var4 = new zars("Reference", "");
      com.aspose.cells.b.a.a.ze.a(var4.b, (Object)(new zakr("URI", '/' + var2.b() + "?ContentType=application/vnd.openxmlformats-package.relationships+xml")));
      zars var5 = new zars("Transforms", "");
      var4.a((zart)var5);
      zars var6 = new zars("Transform", "");
      com.aspose.cells.b.a.a.ze.a(var6.b, (Object)(new zakr("Algorithm", "http://schemas.openxmlformats.org/package/2006/RelationshipTransform")));
      var5.a((zart)var6);
      zarq var7 = new zarq("Transform");
      com.aspose.cells.b.a.a.ze.a(var7.a, (Object)(new zakr("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315")));
      var5.a((zart)var7);
      zcfl var8 = var3.f();

      String var11;
      for(int var9 = 0; var9 < var8.j().a(); ++var9) {
         zcfl var10 = (zcfl)var8.j().a(var9);
         var11 = zarv.a(var10, "TargetMode");
         if (var11 == null || !var11.startsWith("External")) {
            String var12 = zarv.a(var10, "Target");
            if (var12 != null && var12.length() > 0 && !var12.startsWith("docProps/")) {
               zarq var13 = new zarq("mdssi:RelationshipReference");
               com.aspose.cells.b.a.a.ze.a(var13.a, (Object)(new zakr("SourceId", zarv.a(var10, "Id"))));
               var6.a((zart)var13);
            }
         }
      }

      String var16 = "";
      HashMap var17 = zarv.a;
      synchronized(zarv.a) {
         zarv.a();
         var11 = "";
         String[] var20 = new String[]{var11};
         var4.a(var20);
         var11 = var20[0];
         String[] var22 = new String[]{var16};
         zarv.a(var11, var22, var1);
         var16 = var22[0];
      }

      zarq var18 = new zarq("DigestMethod");
      com.aspose.cells.b.a.a.ze.a(var18.a, (Object)(new zakr("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1")));
      var4.a((zart)var18);
      byte[] var19 = com.aspose.cells.b.a.g.ze.getUTF8().a(var16);
      zbbm var10000 = this.b;
      byte[] var21 = zbbm.b(var19);
      zars var23 = new zars("DigestValue", com.aspose.cells.b.a.ze.a(var21));
      var4.a((zart)var23);
      return var4;
   }

   private void a(zars var1) {
      com.aspose.cells.b.a.a.zd var2 = new com.aspose.cells.b.a.a.zd();
      Iterator var3 = var1.d.iterator();

      while(var3.hasNext()) {
         zars var4 = (zars)var3.next();
         zakr var5 = (zakr)var4.b.get(0);
         var2.a(var5.b, var4);
      }

      var1.d.clear();
      var3 = var2.c().iterator();

      while(var3.hasNext()) {
         Object var6 = var3.next();
         var1.a((zart)((zars)var2.d(var6)));
      }

   }

   private zars a(com.aspose.cells.a.f.zi var1) throws Exception {
      zars var2 = new zars("Object", "");
      com.aspose.cells.b.a.a.ze.a(var2.b, (Object)(new zakr("Id", "idPackageObject")));
      com.aspose.cells.b.a.a.ze.a(var2.b, (Object)(new zakr("xmlns:mdssi", "http://schemas.openxmlformats.org/package/2006/digital-signature")));
      zars var3 = new zars("Manifest", "");
      zars var4 = this.a();
      var2.a((zart)var3);
      var2.a((zart)var4);
      Iterator var5 = var1.iterator();

      while(var5.hasNext()) {
         com.aspose.cells.a.f.zh var6 = (com.aspose.cells.a.f.zh)var5.next();
         if (var6.b().contains("_rels/") && !var6.c()) {
            var3.a((zart)this.a(var1, var6));
         }
      }

      com.aspose.cells.a.f.zh var15 = var1.a("[Content_Types].xml");
      zcfk var16 = new zcfk();
      var16.a(var1.a(var15));
      zcfl var7 = var16.f();
      HashMap var8 = new HashMap();
      HashMap var9 = new HashMap();

      for(int var10 = 0; var10 < var7.j().a(); ++var10) {
         zcfl var11 = (zcfl)var7.j().a(var10);
         String var12;
         String var13;
         if ("Override".equals(var11.i())) {
            var12 = zarv.a(var11, "PartName");
            var13 = zarv.a(var11, "ContentType");
            if (!var12.startsWith("/")) {
               var12 = "/" + var12;
            }

            var8.put(com.aspose.cells.b.a.zv.a(var12, '/'), var12 + "?ContentType=" + var13);
         } else if ("Default".equals(var11.i())) {
            var12 = zarv.a(var11, "Extension");
            var13 = zarv.a(var11, "ContentType");
            var9.put("." + var12, "?ContentType=" + var13);
         }
      }

      try {
         Iterator var17 = var1.iterator();

         while(var17.hasNext()) {
            com.aspose.cells.a.f.zh var18 = (com.aspose.cells.a.f.zh)var17.next();
            if (!var18.b().startsWith("docProps/") && !var18.b().endsWith(".rels") && !"[Content_Types].xml".equals(var18.b()) && var18.b().startsWith("xl/")) {
               if (var8.get(var18.b()) != null) {
                  var3.a((zart)this.a(var1, var18.b(), (String)var8.get(var18.b())));
               } else if (var9.get(com.aspose.cells.b.a.d.zk.d(var18.b())) != null) {
                  var3.a((zart)this.a(var1, var18.b(), "/" + var18.b() + (String)var9.get(com.aspose.cells.b.a.d.zk.d(var18.b()))));
               }
            }
         }
      } catch (Exception var14) {
         ;
      }

      this.a(var3);
      return var2;
   }

   private zars b(DigitalSignature var1) {
      String var2 = "<SignatureProperties><SignatureProperty Id=\"idOfficeV1Details\" Target=\"#idPackageSignature\"><SignatureInfoV1 xmlns=\"http://schemas.microsoft.com/office/2006/digsig\"><SetupID></SetupID><SignatureText></SignatureText><SignatureImage></SignatureImage><SignatureComments>" + var1.getComments() + "</SignatureComments>" + "<WindowsVersion>5.1</WindowsVersion><OfficeVersion>12.0</OfficeVersion><ApplicationVersion>12.0</ApplicationVersion>" + "<Monitors>1</Monitors><HorizontalResolution>1280</HorizontalResolution><VerticalResolution>800</VerticalResolution>" + "<ColorDepth>32</ColorDepth><SignatureProviderId>{00000000-0000-0000-0000-000000000000}</SignatureProviderId>" + "<SignatureProviderUrl></SignatureProviderUrl><SignatureProviderDetails>9</SignatureProviderDetails>" + "<ManifestHashAlgorithm>http://www.w3.org/2000/09/xmldsig#sha1</ManifestHashAlgorithm><SignatureType>1</SignatureType>" + "</SignatureInfoV1></SignatureProperty></SignatureProperties>";
      zars var3 = new zars("Object", var2);
      com.aspose.cells.b.a.a.ze.a(var3.b, (Object)(new zakr("Id", "idOfficeObject")));
      return var3;
   }

   private zarp a(com.aspose.cells.a.f.zi var1, DigitalSignature var2, int var3) throws Exception {
      this.d = var2;
      zarp var4 = new zarp();
      zars var5 = new zars("Signature", "");
      com.aspose.cells.b.a.a.ze.a(var5.b, (Object)(new zakr("Id", "idPackageSignature")));
      com.aspose.cells.b.a.a.ze.a(var5.b, (Object)(new zakr("xmlns", "http://www.w3.org/2000/09/xmldsig#")));
      zars var6 = this.a(var1);
      zars var7 = this.b(var2);
      zars var8 = this.a(var6, var7);
      var5.a((zart)var8);
      var5.a((zart)this.a(var2, var8));
      var5.a((zart)this.a(var2));
      var5.a((zart)var6);
      var5.a((zart)var7);
      var4.a((zart)var5);
      return var4;
   }

   private String c(byte[] var1) {
      String var2 = com.aspose.cells.b.a.ze.a(var1);
      String var3 = "";

      while(true) {
         String var4 = var2.substring(0, 0 + Math.min(72, var2.length()));
         var3 = var3 + "\r\n    " + var4;
         if (var4.length() < 72) {
            return var3;
         }

         var2 = var2.substring(72);
      }
   }

   private zars a(DigitalSignature var1, zars var2) {
      String var3 = "";
      String var4 = "";
      String[] var5 = new String[]{var4};
      var2.a(var5);
      var4 = var5[0];
      HashMap var6 = zarv.a;
      synchronized(zarv.a) {
         zarv.a();
         String[] var7 = new String[]{var3};
         zarv.a(var4, var7);
         var3 = var7[0];
      }

      zbbm var10 = new zbbm(var1.b());
      byte[] var11 = var10.a(com.aspose.cells.b.a.g.ze.getUTF8().a(var3));
      zars var8 = new zars("SignatureValue", this.c(var11));
      return var8;
   }

   private zars a(zars var1, zars var2) {
      String var3 = "";
      String var4 = "";
      String[] var5 = new String[]{var4};
      var1.a(var5);
      var4 = var5[0];
      HashMap var6 = zarv.a;
      synchronized(zarv.a) {
         zarv.a();
         String[] var7 = new String[]{var3};
         zarv.a(var4, var7);
         var3 = var7[0];
      }

      zbbm var10000 = this.b;
      byte[] var12 = zbbm.b(com.aspose.cells.b.a.g.za.getASCII().a(var3));
      var4 = "";
      var3 = "";
      var5[0] = var4;
      var2.a(var5);
      var4 = var5[0];
      HashMap var13 = zarv.a;
      synchronized(zarv.a) {
         zarv.a();
         String[] var8 = new String[]{var3};
         zarv.a(var4, var8);
         var3 = var8[0];
      }

      var10000 = this.b;
      byte[] var14 = zbbm.b(com.aspose.cells.b.a.g.za.getASCII().a(var3));
      String var15 = "<CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><Reference URI=\"#idPackageObject\" Type=\"http://www.w3.org/2000/09/xmldsig#Object\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>" + com.aspose.cells.b.a.ze.a(var12) + "</DigestValue></Reference>" + "<Reference URI=\"#idOfficeObject\" Type=\"http://www.w3.org/2000/09/xmldsig#Object\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>" + "<DigestValue>" + com.aspose.cells.b.a.ze.a(var14) + "</DigestValue></Reference>";
      zars var9 = new zars("SignedInfo", var15);
      return var9;
   }

   private zarr b(com.aspose.cells.a.f.zi var1) throws Exception {
      zarr var2 = new zarr("_xmlsignatures/");
      int var3 = 0;
      Iterator var4 = this.a.iterator();

      while(var4.hasNext()) {
         DigitalSignature var5 = (DigitalSignature)var4.next();
         ++var3;
         zaru var6 = new zaru("_xmlsignatures/sig" + var3 + ".xml");
         var6.a((zart)this.a(var1, var5, var3));
         var2.a((zart)var6);
      }

      return var2;
   }

   private zarr a(zarr var1) {
      zars var2 = new zars("Relationships", "");
      com.aspose.cells.b.a.a.ze.a(var2.b, (Object)(new zakr("xmlns", "http://schemas.openxmlformats.org/package/2006/relationships")));
      boolean var3 = false;
      Iterator var4 = var1.d.iterator();

      zaru var5;
      while(var4.hasNext()) {
         var5 = (zaru)var4.next();
         com.aspose.cells.b.a.g.a.zc var6 = this.e.a(var5.c);
         if (var6.b().a() == 3) {
            String var7 = var6.b().a(0).b();
            String var8 = "rId" + var6.b().a(1).b();
            zarq var9 = new zarq("Relationship");
            com.aspose.cells.b.a.a.ze.a(var9.a, (Object)(new zakr("Id", var8)));
            com.aspose.cells.b.a.a.ze.a(var9.a, (Object)(new zakr("Type", "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature")));
            com.aspose.cells.b.a.a.ze.a(var9.a, (Object)(new zakr("Target", var7)));
            var2.a((zart)var9);
         }
      }

      var1.a((zart)(new zaru("_xmlsignatures/origin.sigs")));
      zarr var10 = new zarr("_xmlsignatures/_rels/");
      var1.a((zart)var10);
      var5 = new zaru("_xmlsignatures/_rels/origin.sigs.rels");
      var10.a((zart)var5);
      zarp var11 = new zarp("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
      var5.a((zart)var11);
      var11.a((zart)var2);
      return var1;
   }

   public static void a(InputStream var0, OutputStream var1) throws IOException {
      int var2;
      while((var2 = var0.read(f)) != -1) {
         var1.write(f, 0, var2);
      }

   }

   private void b(com.aspose.cells.b.a.d.zm var1) throws Exception {
      com.aspose.cells.a.f.zi var2 = com.aspose.cells.a.f.zi.a(var1);
      com.aspose.cells.b.a.d.zh var3 = new com.aspose.cells.b.a.d.zh();
      com.aspose.cells.a.f.zj var4 = new com.aspose.cells.a.f.zj(var3);
      var4.b(2);
      var4.d(0);
      zarr var5 = this.b(var2);
      this.a(var5);
      zarx var6 = new zarx(var4);
      var5.a(var6);
      this.a(var5, var2, var4);
      this.b(var5, var2, var4);
      var4.b();
      var4.l();
      new com.aspose.cells.a.f.zj(var1);
      Iterator var8 = var2.iterator();

      while(var8.hasNext()) {
         com.aspose.cells.a.f.zh var9 = (com.aspose.cells.a.f.zh)var8.next();
         if (var9.a() > 0L && !var9.b().startsWith("_rels/") && !var9.b().startsWith("_xmlsignatures/") && !var9.b().startsWith("[Content_Types].xml")) {
            com.aspose.cells.a.f.zh var10 = var4.a(var9.b());
            var10.a(var9.d());
            var4.a(var10);
            a(var2.a(var9).g(), var4.j());
            var4.l();
         }
      }

      var4.f_();
      var2.b();
      var1.b(0L);
      var1.a(0L);
      var3.b(0L);
      a(var3.g(), var1.j());
      var1.b();
      var3 = null;
   }

   private void a(zarr var1, com.aspose.cells.a.f.zi var2, com.aspose.cells.a.f.zj var3) throws Exception {
      zaru var4 = new zaru("_rels/.rels");
      var4.a(var2, "_rels/.rels");
      zarq var5 = new zarq("Relationship");
      com.aspose.cells.b.a.a.ze.a(var5.a, (Object)(new zakr("Id", "rId")));
      com.aspose.cells.b.a.a.ze.a(var5.a, (Object)(new zakr("Type", "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin")));
      com.aspose.cells.b.a.a.ze.a(var5.a, (Object)(new zakr("Target", "_xmlsignatures/origin.sigs")));
      var4.b("/Relationships/", var5);
      zarx var6 = new zarx(var3);
      var4.a(var6);
   }

   private void a(zaru var1) {
      ArrayList var2 = new ArrayList();
      zarp var3 = (zarp)var1.d.get(0);
      zars var4 = (zars)((zars)var3.d.get(0));
      Iterator var5 = var4.d.iterator();

      while(true) {
         zarq var6;
         while(var5.hasNext()) {
            var6 = (zarq)var5.next();
            Iterator var7 = var6.a.iterator();

            while(var7.hasNext()) {
               zakr var8 = (zakr)var7.next();
               if (var8.b.startsWith("application/vnd.openxmlformats-package.digital-signature-")) {
                  com.aspose.cells.b.a.a.ze.a(var2, (Object)var6);
                  break;
               }
            }
         }

         var5 = var2.iterator();

         while(var5.hasNext()) {
            var6 = (zarq)var5.next();
            var4.d.remove(var6);
         }

         return;
      }
   }

   private void b(zarr var1, com.aspose.cells.a.f.zi var2, com.aspose.cells.a.f.zj var3) throws Exception {
      zaru var4 = new zaru("[Content_Types].xml");
      var4.a(var2, "[Content_Types].xml");
      this.a(var4);
      zarq var5 = new zarq("Default");
      com.aspose.cells.b.a.a.ze.a(var5.a, (Object)(new zakr("Extension", "sigs")));
      com.aspose.cells.b.a.a.ze.a(var5.a, (Object)(new zakr("ContentType", "application/vnd.openxmlformats-package.digital-signature-origin")));
      var4.a((String)"/Types/", (zart)var5);
      Iterator var6 = var1.d.iterator();

      while(var6.hasNext()) {
         zart var7 = (zart)var6.next();
         if (var7 instanceof zaru) {
            zaru var8 = (zaru)var7;
            if (var8.c.startsWith("_xmlsignatures/sig")) {
               zarq var9 = new zarq("Override");
               com.aspose.cells.b.a.a.ze.a(var9.a, (Object)(new zakr("PartName", "/" + var8.c)));
               com.aspose.cells.b.a.a.ze.a(var9.a, (Object)(new zakr("ContentType", "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml")));
               var4.a((String)"/Types/", (zart)var9);
            }
         }
      }

      zarx var10 = new zarx(var3);
      var4.a(var10);
   }
}
